Untyped files


Untyped file is a kind of files that can be used with any file to perform common
file tasks such coping, encryption, compression, and file transfere. These kind of
file operations does not care about file type or data type in the file, so that untyped
file is very suitable for such files.

There is no transformation in untyped file, in other meaning when you read a buffer
from file, data format in the buffer will be the same format in file.


Untyped file in Object Pascal

Untyped file in Object Pascal declared as file, reading, writing, and other manipulation
described below:

---------------------------------------------------------------------------------------------------------------------------------------------------------
Operation                            
proc/func
---------------------------------------------------------------------------------------------------------------------------------------------------------
Declaration                           var  F: file;
Assigning                             AssignFile(F, AFileName);
Opening for reading              
Reset(F, RecordSize); or Rewrite(F, RecordSize);
Opening for writing                
Rewrite(F, RecordSize) or Reset(F, RecordSize)

                 
with FileMode = 2
Opening for read/write          
Reset(F, RecordSize) with FileMode = 2
Appending                          
FileMode:= 2; Reset(F, RecordSize);
                 Seek(F, FileSize(F));

Reading                              
BlockRead(F, Buf, Count, [NumRead]);
Writing                                
BlockWrite(F, Buf, Count, [NumWritten]);
Goto random position            
Seek(F, Position); { Position is a number of desired
record }

Closing file                          
CloseFile(F);
Getting records count            
FileSize(F);
Getting current position         FilePos(F);
---------------------------------------------------------------------------------------------------------------------------------------------------------

Untyped files organized as random access files so that you can open it in reading/writing
mode and you can goto any record randomly using
Seek procedure.

Reset in untyped file opens file for reading, writing, or reading and writing mode
according to FileMode variable:

If FileMode = 0  that means read only
If FileMode = 1  that means write only
If FileMode = 2  that means read and write access

Befor openning file with reset make sure that the file is exists.

Rewrite in typed file create a new file and it overwrite the file if it is already
exists. When you create file with Rewrite you can read and write from and to file.

Append does not work with typed files, insead you must open file with reset and
goto beyond last record such as:

FileMode:= 2;  // Read and write mode
Reset(F, 1);
Seek(F, FileSize(F));

If file size if 5 that means there is 5 records in file from 0 to 4 so that going
to record number 5 will ensures writing on an empty record. This works only if file
exists but it will rais an exception if file does not exists, so that you must write:

AssignFile(F, FileName);
FileMode:= 2;  
// Read and write mode
if FileExists(FileName) then
Reset(F)
else
Rewrite(F);  
// Create new file
Seek(F, FileSize(F));

You must determine record size when openning untyped file with reset or rewrite
such as:

Reset(F, 1);  // Record size = 1 byte
Rewrite(F, SizeOf(Rec)); // Record size = size of Rec variable


Example:

{ Function of file copy }

function
FCopy(SourceName, TargetName: string): boolean;
var
SourceF, TargetF: file;
Buf: array [0 .. 1023] of byte;
NumRead: integer;
begin
Result:= false;
try
  AssignFile(SourceF, SourceName);
  AssignFile(TargetF, TargetName);
  FileMode:= 0;
// Read only for source file
   Reset(SourceF, 1); // Record size = 1 byte
   Rewrite(TargetF, 1); // Create target file (destination)

 
(*** Coping ***)
   while not Eof(SourceF) do
  begin
    BlockRead(SourceF, Buf, SizeOf(Buf), NumRead);
    BlockWrite(TargetF, Buf, NumRead);
  end;
// while not Eof..
  CloseFile(SourceF);
  CloseFile(TargetF);
  Result:= True;

except
  on E: EInOutError do
  begin
    MessageDlg(E.Message, mtError, [mbOk], 0);
    exit;
  end; // on E:..
end; // try
end;


{ Example of using FCopy function }

if FCopy('c:\autoexec.bat', 'c:\autoexec.foo') then
ShowMessage('1 file copied')
else
ShowMessage('Error while copy file');

Notes:

- There is no transformation in untyped file, buffer are read from disk to RAM without
any modification, moreover you can read more than one record at a time, as the example
above you can read up to 1024 record at one time, these two features make untyped
file the fastest file type in Object Pascal and make it the best choice of coping,
encryption, compression, and similar file manipulation.


See also:

Copy files
Text files
Untyped files